<?php
/**
 * Skyline :: Patterns & component framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Skyline\Structure;
use OverflowException;

/**
 * Abstract implementation of ContainerInterface
 * 
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
abstract class AbstractContainer implements BoundContainerInterface
{
	/**
	 * @var int
	 */
	protected $capacity = -1;
	
	/**
	 * Returns the maximum number of elements the container can hold
	 * @return int
	 */
	public function getCapacity()
	{
		return $this->capacity;
	}
	
	/**
	 * Sets the maximum number of elements the container can hold
	 * 
	 * @param int $capacity
	 * @return void
	 */
	public function setCapacity($capacity)
	{
		$this->capacity = (int)$capacity;
	}
	
	/**
	 * Returns true if this container is empty
	 * @return bool
	 */
	public function isEmpty()
	{
		return ($this->count() == 0);
	}
	
	/**
	 * Returns true if this container is at capacity
	 * @return bool
	 */
	public function isFull()
	{
		return ($this->capacity > 0 && $this->count() == $this->capacity);
	}
	
	/**
	 * @throws OverflowException If container is at capacity
	 * @return void
	 */
	protected function assertNotFull()
	{
		if($this->isFull()) {
			throw new OverflowException('Container has reached capacity');
		}
	}
}